package com.easywan.web.controller.users;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easywan.core.BaseController;
import com.easywan.pojo.IResult;
import com.easywan.pojo.IResultSupport;
import com.easywan.service.IBaseUsersService;
import com.easywan.service.IRToosName2BaseUsersSerivce;
import com.easywan.service.IToolsNameService;
import com.easywan.vo.BaseUsers;
import com.easywan.vo.ToolsName;
@Controller
@RequestMapping("users")
public class BaseUsersController extends BaseController{
	private static Logger logger = Logger.getLogger(BaseUsersController.class);
	@Autowired
	private IToolsNameService toolsNameService;
	@Autowired
	IBaseUsersService usersService;
	@RequestMapping("index")
	public String index(){
		return "users/index.jsp";
	}
	@ResponseBody
	@RequestMapping("toolsAdd")
	public String toolsAdd(HttpServletRequest request){
		IResult result = null;
		try{
			BaseUsers users = getUserInfo(request, "");
			String toolsName = getString(request, "name");
			paramsNotNull(toolsName);
			result = usersService.toolsAdd(toolsName, users.getId());
			IResultSupport.setModel(result, "/users/index.html");
		}
		catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			result = IResultSupport.buildResult(IResult.EXCEPTION_CODE, e.getMessage());
		}
		
		return toJSON(result);
	}
	@ResponseBody
	@RequestMapping("toolsList")
	public String toolsList(HttpServletRequest request){
		IResult result = null;
		try{
			List<ToolsName> tools = toolsNameService.getUsersTools(getUsersId(request));
			result = IResultSupport.buildResult(IResult.SUCCESS_CODE, tools);
		}catch(Exception e){
			logger.error("获取工具列表错误:"+e);
			result = IResultSupport.buildResult(IResult.EXCEPTION_CODE, "服务器异常");
		}
		return toJSON(result);
	}
	
}
